﻿$(function () {
    loadMenu();

    var query = new breeze.EntityQuery().from("Jobs")
        .where("Status","==", 1)
        .skip(0)
        .orderBy("UniqueId")
        .take(recordonPage).inlineCount();
    queryWithCondition(query);
});
$('#_loading').show();
var recordonPage = 10; // You can modify this
vm.pagenumber = ko.observable(1);
vm.maxPage = ko.observable();
vm.Joblist = ko.observableArray();

vm.selectIndustry = ko.observable('');
vm.selectFunction = ko.observable('');
vm.selectCountry = ko.observable('');
vm.selectProvince = ko.observable();
vm.selectCity = ko.observable('');

vm.Industry = ko.observableArray();
vm.Country = ko.observableArray();
// Cant prevent bubble subcribe
vm.pagenumber.subscribe(function (newValue) {
    if (parseInt(newValue) < 1) {
        vm.pagenumber(1);
        newValue = 1;
    }  
    if (parseInt(newValue) > vm.maxPage()) {
        vm.pagenumber(vm.maxPage());
        newValue = vm.maxPage();
    }
    if (parseInt(newValue) > 0 && parseInt(newValue) <= vm.maxPage()) {
        getJobListonCrit();
    }    
});


var industryQuery = new breeze.EntityQuery().from("Industries").where("ParentId","==", null);
manager.executeQuery(industryQuery).then(function (data) {
    data.results.forEach(function (item) {
        vm.Industry.push(item);
    })
}).fail(function (e) {
    alert(e);
});

vm.selectIndustry.subscribe(function (Industry) {
    if (typeof Industry == 'undefined')
        vm.selectFunction('');
    
});
vm.selectCountry.subscribe(function (Country) {
    if (typeof Country == 'undefined')
        vm.selectProvince('');

});
vm.selectProvince.subscribe(function (Province) {
    if (typeof Province == 'undefined')
        vm.selectCity('');
});

var locationQuery = new breeze.EntityQuery().from("Locations").where("ParentId", "==", null);
manager.executeQuery(locationQuery).then(function (data) {
    data.results.forEach(function (item) {
        vm.Country.push(item);
    })
}).fail(function (e) {
    alert(e);
});



vm.firstPage = function () {
    vm.pagenumber(1);
}
vm.nextPage = function () {
    vm.pagenumber(vm.pagenumber() + 1);
}
vm.prevPage = function () {
    vm.pagenumber(vm.pagenumber() - 1);
}
vm.lastPage = function () {
    vm.pagenumber(vm.maxPage());
}



function getJobListonCrit() {
    $('#_loading').show();
    vm.Joblist.removeAll();
    var query = new breeze.EntityQuery().from("Jobs")
        .where("Status", "==", true)
        .skip(recordonPage * (vm.pagenumber() - 1))
        .orderBy("UniqueId")
        .take(recordonPage).inlineCount();
    manager.executeQuery(query).then(function (data) {
        data.results.forEach(function (item) {
            if (item.SalaryFrom() == 'Competitive') {
                item.StandardSalary = addCommas(item.SalaryFrom());
            } else {
                item.StandardSalary = addCommas(item.SalaryFrom()) + " - " + addCommas(item.SalaryTo());
            }
            
            vm.Joblist.push(item);
        })
        var Pages = Math.ceil((parseFloat(data.inlineCount) / recordonPage).toFixed(2));
        vm.maxPage(Pages);
        addPagination(vm.maxPage());
        $('#_loading').hide();
    }).fail(function (e) {
        alert(e);
    });
}

function slidedown() {
    $('#_down').hide();
    $('#_up').show();

    $('#_searchform').slideDown();

}
function slideup() {
    $('#_up').hide();
    $('#_down').show();
    $('#_searchform').slideUp();
}

function queryWithCondition(query) {
    manager.executeQuery(query).then(function (data) {
        data.results.forEach(function (item) {
            if (item.SalaryFrom() == 'Competitive') {
                item.StandardSalary = addCommas(item.SalaryFrom());
            } else {
                item.StandardSalary = addCommas(item.SalaryFrom()) + " - " + addCommas(item.SalaryTo());
            }
            
            vm.Joblist.push(item);
        })
        if (data.results.length > 0) {
            var Pages = Math.ceil((parseFloat(data.inlineCount) / recordonPage).toFixed(2));
            vm.maxPage(Pages);

            ko.cleanNode($('#body')[0]);
            ko.applyBindings(vm, $('#body')[0]);

            addPagination(vm.maxPage());
        }
        
        $('#_loading').hide();
    }).fail(function (e) {
        alert(e);
    });
}

function _Search() {
    vm.Joblist.removeAll();
    var jobname = $.trim($('#_searchJobname').val());
    var salaryFrom = ($.trim($('#_searchSalaryFr').val()).split(',').join(''));
    var salaryTo = ($.trim($('#_searchSalaryTo').val()).split(',').join(''));
    var _function = '';
    var _city = '';
    if (typeof vm.selectFunction() != 'undefined' && vm.selectFunction() != "") _function = vm.selectFunction().UniqueId();
    if (typeof vm.selectCity() != 'undefined' && vm.selectCity() != "") _city = vm.selectCity().UniqueId();

    $('#_loading').show();
    var preds = Array();
    if (jobname != "") {
        var p1 = new breeze.Predicate.create("Name", "Contains", jobname);
        preds.push(p1);
    }
    if (salaryFrom != "") {
        var p2 = new breeze.Predicate.create("SalaryFrom", ">=", salaryFrom);
        preds.push(p2);
    }
    if (salaryTo != "") {
        var p3 = new breeze.Predicate.create("SalaryTo", "<=", salaryTo);
        preds.push(p3);
    }
    if (_function != "") {
        var p4 = new breeze.Predicate.create("IndustryId", "==", _function);
        preds.push(p4);
    }
    if (_city != "") {
        var p5 = new breeze.Predicate.create("LocationId", "==", _city);
        preds.push(p5);
    }
    var postdate = $("input[type='radio'][name='optionsRadios']:checked");
    if (typeof postdate.val() != 'undefined' && postdate.val() != 'all') {
        var time = new Date($.now());
        time.setDate(time.getDate() - postdate.val());
        var p6 = new breeze.Predicate.create("DatePost", ">=", time);
        preds.push(p6);
    }


    var newPred = new breeze.Predicate.and(preds);
    if (preds.length == 0) {
        var query = new breeze.EntityQuery().from("Jobs")
        .skip(0)
        .orderBy("UniqueId")
        .take(recordonPage).inlineCount();
    } else {
        var query = new breeze.EntityQuery().from("Jobs")
       .where(newPred)
       .skip(0)
       .orderBy("UniqueId")
       .take(recordonPage).inlineCount();

        
    }

    queryWithCondition(query);


}
